var formApp = new Vue({
    el:'#formApp',
    data:{
        address_id:0,
        address:{
            province:null,
            city:null,
            district: null,
            street: null,
            consignee: null,
            phone:null,
            isdefault:false
        },
        formattedArea:''
    },
    methods:{
        submit:function () {
            var self = this;
            if (!this.address.province || !this.address.city || !this.address.district){
                weui.topTips('请选择区域');
                return false;
            }

            if (!this.address.street){
                weui.topTips('请填写街道地址');
                return false;
            }

            if (!this.address.consignee){
                weui.topTips('请填写姓名');
                return false;
            }

            if (!this.address.phone){
                weui.topTips('请填写手机号码');
                return false;
            }
            this.address.isdefault = this.address.isdefault ? 1 : 0;
            $.ajax({
                type:'POST',
                url: '/h5/saveaddress',
                data:{address:this.address,address_id:this.address_id},
                success:function (response) {
                    if (self.address_id){
                        weui.toast('地址修改成功',{callback:DSXUtil.reFresh});
                    } else {
                        weui.toast('地址添加成功',{callback:DSXUtil.reFresh});
                    }
                }
            });
        },
        del:function (address_id) {
            weui.confirm('确定要删除本地址吗?', function () {
                var loading;
                $.ajax({
                    url:'/h5/deladdress',
                    data:{address_id:address_id},
                    beforeSend:function () {
                        loading = weui.loading('正在删除..');
                    },
                    success:function () {
                        loading.hide(function () {
                            DSXUtil.reFresh();
                        });
                    }
                });
            });
        }
    }
});

function showPicker() {
    $.ajax({
        url:'/h5/getdistrictjson',
        success:function (response) {
            weui.picker(response, {
                onConfirm:function (result) {
                    formApp.address.province = result[0].label;
                    formApp.address.city = result[1].label;
                    formApp.address.district = result[2].label;
                    formApp.formattedArea = result[0].label +'/'+ result[1].label +'/'+ result[2].label;
                },
                defaultValue:[formApp.address.province, formApp.address.city, formApp.address.district]
            });
        }
    });
}

function showForm() {
    $("#formApp").show();
}

function hideForm() {
    $("#formApp").hide();
}

function editAddress(address_id){
    $.ajax({
        url:'/h5/getaddress',
        data:{address_id:address_id},
        success:function (response) {
            formApp.address_id = address_id;
            formApp.address = response.address;
            formApp.formattedArea = response.address.province +'/'+response.address.city +'/'+response.address.district;
            showForm();
        }
    });
}

$("#area").on('focus', function () {
    document.activeElement.blur();
    showPicker();
});
